/**
 * 
 */
package unitTests;


import junit.framework.TestCase;
import mainClasses.*;
import pieces.*;

/**
 * @author jmikhail
 *
 */
public class BoardTest extends TestCase {

	Board testingBoard1 = new Board();
	
	
	/**
	 * @param name
	 */
	public BoardTest(String name) {
		super(name);
	}

	

	
	
	/*
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp(){
		testingBoard1.setUp();
	}

	
	
	
	
	/*
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown(){
		@SuppressWarnings("unused")
		Board testingBoard1 = new Board();
	}

	
	
	
	/**
	 * Test method for {@link mainClasses.Board#Board()}
	 */
	public void testBoard() {
		Board testingBoard2 = new Board();
		for(int i=0; i<Board.SIZE; i++){
			for(int j=0; j<Board.SIZE; j++){
				assertTrue(testingBoard2.getItems()[i][j] instanceof NullPiece);
			}
		}
	}


	
	
	
	/**
	 * Test method for {@link mainClasses.Board#setUp()}
	 */
	public void testSetUp() {
		assertTrue(testingBoard1.getItems()[1][0] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[1][1] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[1][2] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[1][3] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[1][4] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[1][5] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[1][6] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[1][7] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[0][0] instanceof Rook);
		assertTrue(testingBoard1.getItems()[0][1] instanceof Knight);
		assertTrue(testingBoard1.getItems()[0][2] instanceof Bishop);
		assertTrue(testingBoard1.getItems()[0][3] instanceof Queen);
		assertTrue(testingBoard1.getItems()[0][4] instanceof King);
		assertTrue(testingBoard1.getItems()[0][5] instanceof Bishop);
		assertTrue(testingBoard1.getItems()[0][6] instanceof Knight);
		assertTrue(testingBoard1.getItems()[0][7] instanceof Rook);
		for (int i = 2; i < 6; i++) {
			for (int j = 0; j < Board.SIZE; j++) {
				assertTrue(testingBoard1.getItems()[i][j] instanceof NullPiece);
			}
		}
		assertTrue(testingBoard1.getItems()[6][0] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[6][1] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[6][2] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[6][3] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[6][4] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[6][5] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[6][6] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[6][7] instanceof Pawn);
		assertTrue(testingBoard1.getItems()[7][0] instanceof Rook);
		assertTrue(testingBoard1.getItems()[7][1] instanceof Knight);
		assertTrue(testingBoard1.getItems()[7][2] instanceof Bishop);
		assertTrue(testingBoard1.getItems()[7][3] instanceof Queen);
		assertTrue(testingBoard1.getItems()[7][4] instanceof King);
		assertTrue(testingBoard1.getItems()[7][5] instanceof Bishop);
		assertTrue(testingBoard1.getItems()[7][6] instanceof Knight);
		assertTrue(testingBoard1.getItems()[7][7] instanceof Rook);
	}

	
	
	
	/**
	 * Test method for {@link mainClasses.Board#remove(java.awt.Point)}.
	 */
	public void testRemove() {
		assertTrue(testingBoard1.getItems()[0][0] instanceof Rook);
		testingBoard1.remove(0,0);
		assertFalse(testingBoard1.getItems()[0][0] instanceof Rook);
		assertTrue(testingBoard1.getItems()[0][0] instanceof NullPiece);
		
		assertTrue(testingBoard1.getItems()[4][4] instanceof NullPiece);
		testingBoard1.remove(4,4);
		assertTrue(testingBoard1.getItems()[4][4] instanceof NullPiece);
	}

	
	
	
	/**
	 * Test method for {@link mainClasses.Board#getPiece(java.awt.Point)}.
	 */
	public void testGetPiece() {
		assertTrue(testingBoard1.getPiece(1,1) instanceof Pawn);
		assertTrue(testingBoard1.getPiece(0,0) instanceof Rook);
		assertTrue(testingBoard1.getPiece(5,5) instanceof NullPiece);
		assertFalse(testingBoard1.getPiece(0,0) instanceof NullPiece);
	}

	
	
	
	/**
	 * Test method for {@link mainClasses.Board#swap(java.awt.Point, java.awt.Point)}.
	 */
	public void testSwap() {
		Board testingBoard5 = new Board();
		testingBoard5.add(new King(0, 0, testingBoard5, "Black"));
		testingBoard5.add(new King(7, 7, testingBoard5, "White"));
		testingBoard5.add(new Queen(6, 1, testingBoard5, "White"));
		
		assertTrue(testingBoard5.getItems()[6][4] instanceof NullPiece); //Make sure that the position 6,4 is empty
		testingBoard5.swap(6, 1, 6, 4);	//Moving the queen
		assertTrue(testingBoard5.getItems()[6][4] instanceof Queen); //Check if the swapping occurred
		
	}

	
	
	
	/**
	 * Test method for {@link mainClasses.Board#knightSwap(java.awt.Point, java.awt.Point)}.
	 */
	public void testKnightSwap() {
		Board testingBoard7 = new Board();
		testingBoard7.add(new Knight(0, 0, testingBoard7, "Black"));
		
		testingBoard7.add(new Knight(0, 1, testingBoard7, "White"));
		testingBoard7.add(new Knight(1, 1, testingBoard7, "White"));
		testingBoard7.add(new Knight(1, 0, testingBoard7, "White"));	//Surround the knight by pawns
		
		testingBoard7.knightSwap(0, 0, 2, 1);		
		assertTrue(testingBoard7.getItems()[2][1] instanceof Knight);	//Check if the knight moved through the pawns
		assertTrue(testingBoard7.getItems()[0][0] instanceof NullPiece);//Check that the origin of the move has a NullPiece
		assertFalse(testingBoard7.getItems()[0][0] instanceof Knight);	//Check that the origin of the move no longer has a knight
		
	}

	
	

	
	/**
	 * Test method for {@link mainClasses.Board#getKing()}.
	 */
	public void testGetKing() {
		testingBoard1.getKing();
		assertEquals(testingBoard1.wking, testingBoard1.getItems()[0][4]);
		assertEquals(testingBoard1.bking, testingBoard1.getItems()[7][4]);
	}

	

	
	
	
	/**
	 * Test method for {@link mainClasses.Board#add(pieces.Piece)}
	 */
	public void testAdd() {
		
		assertTrue(testingBoard1.getItems()[4][4] instanceof NullPiece);
		testingBoard1.add(new Pawn(4, 4, testingBoard1, "white"));
		assertFalse(testingBoard1.getItems()[4][4] instanceof NullPiece);
		assertTrue(testingBoard1.getItems()[4][4] instanceof Pawn);
	}

	
	
	
	/**
	 * Test method for {@link mainClasses.Board#isInDanger(java.awt.Point, java.lang.String)}
	 */
	public void testIsInDanger() {
		Board testingBoard4 = new Board();
		testingBoard4.add(new King(7, 0, testingBoard4, "Black"));
		testingBoard4.add(new Queen(7, 3, testingBoard4, "White"));
		assertTrue(testingBoard4.isInDanger(7, 0, "Black"));	//Checks that the black king is endangered by the white queen
		assertFalse(testingBoard4.isInDanger(7, 0, "White")); //Checks that the black king is not endangered by any black piece
		
		testingBoard4.add(new King(3, 0, testingBoard4, "White"));
		testingBoard4.add(new Rook(3, 6, testingBoard4, "Black"));	
		
		assertTrue(testingBoard4.isInDanger(3, 0, "White")); //Checks that the white King is endangered by the black rook
		
		}

	
	
	
	/**
	 * Test method for {@link mainClasses.Board#whiteKingInDanger()}
	 */
	public void testWhiteKingInDanger() {
		Board testingBoard4 = new Board();
		testingBoard4.add(new King(7, 0, testingBoard4, "Black"));
		assertFalse(testingBoard4.whiteKingInDanger()); //There are no endangered kings
		testingBoard4.add(new Queen(7, 3, testingBoard4, "White"));
		assertFalse(testingBoard4.whiteKingInDanger()); //The black king is the king in danger
		
		
		testingBoard4.add(new King(3, 0, testingBoard4, "White"));
		testingBoard4.add(new Rook(3, 6, testingBoard4, "Black"));	
		
		assertTrue(testingBoard4.whiteKingInDanger()); //The white king is in danger
		
		
	}
	
	public void testIsPawnPromoted(){
		Board testingBoard7 = new Board();
		testingBoard7.add(new King(7, 0, testingBoard7, "Black"));
		testingBoard7.add(new King(0, 0, testingBoard7, "White"));
		
		testingBoard7.add(new Pawn(6, 7, testingBoard7, "White"));
		testingBoard7.swap(6, 7, 7, 7);
		assertTrue(testingBoard7.getPromotion());
		
	}

	
	public void testGetPromotion(){
		Board testingBoard7 = new Board();
		testingBoard7.add(new King(4, 0, testingBoard7, "Black"));
		testingBoard7.add(new King(2, 3, testingBoard7, "White"));
		
		testingBoard7.add(new Pawn(5, 7, testingBoard7, "White"));
		testingBoard7.swap(5, 7, 7, 7);
		assertTrue(testingBoard7.getPromotion());
		
	}
	
	public void testClearPromotion(){
		Board testingBoard7 = new Board();
		testingBoard7.clearPromotion();
		assertFalse(testingBoard7.getPromotion());
		
	}
}
